package regex;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 黄乾坤
 * @create 2022-11-08 11:10
 */
public class HtmlTagUtils {

    public static void main(String[] args) {

        String url = "https://wx-dev.citictrust.com.cn:443/file/anonymous/basic/manage?fr=TMP&amp;path=2022-11-08/TDz4W9.591840b3dd3c40dcb2a3368311323b0e.jpg";
        System.out.println(url);
        Map<String, String> stringStringMap = HttpUtil.decodeParamMap(url, StandardCharsets.UTF_8);
        System.out.println(FileUtil.getSuffix("20221108/1234567.png"));
        System.out.println(stringStringMap);

        String content = "<a class=\\\"image-link\\\" style=\\\"color: rgb(19, 110, 194); text-decoration: none; display: block; width: 220px; height: 264.393px;\\\" " + "title" +
                "=\\\"柳宗元的画像\\\" href=\\\"https://baike.baidu.com/pic/%E6%96%87%E7%AB%A0/5343/0/caae6809201e59fe2eddd4df?fr=lemma&amp;fromModule=lemma_content-image&amp;" + "ct" +
                "=single\\\" target=\\\"_blank\\\" rel=\\\"noopener\\\"><img class=\\\"lazy-img\\\" style=\\\"border: 0px; display: block; margin: 0px auto; width: 220px; " +
                "height: 264.393px;\\\" src=\"data:image/png;base64," +
                "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRF9fX1AAAA0VQI3QAAAAxJREFUeNpiYAAIMAAAAgABT21Z4QAAAABJRU5ErkJggg==\" alt=\\\"柳宗元的画像\\\" data-src=\\\"https://bkimg.cdn.bcebos.com/pic/3b87e950352ac65c7b10a612fbf2b21193138abe?x-bce-process=image/resize,m_lfit,w_440,limit_1\\\"></a><div class=\\\"para\\\" style=\\\"font-size: 14px; overflow-wrap: break-word; color: rgb(51, 51, 51); margin-bottom: 15px; text-indent: 2em; line-height: 24px; zoom: 1;\\\">⑶比喻曲折隐蔽的含义</div>" +
                "<img class=\\\"lazy-img\\\" style=\\\"border: 0px; display: block; margin: 0px auto; width: 220px; height: 264.393px;\\\" data-src=\"https://bkimg.cdn.bcebos" +
                ".com/pic/3b87e950352ac65c7b10a612fbf2b21193138abe?x-bce-process=image/resize,m_lfit,w_440,limit_1\" src=\"https://web-pre.citictrust.com.cn:9013/hsys-manage-console/index.html#/\" alt=\\\"柳宗元的画像\\\" >";
        String reg = "(?i)<img(\\s+[^>]*?)?/?>(.*?</img>)?";
        String srcReg = "(?i) src\\s*=\\s*\"?(.*?)(\"|>|\\s+)";
        String base64Reg = "(?i)^\\s*data:(?:[a-z]+\\/[a-z0-9-+.]+(?:;[a-z-]+=[a-z0-9-]+)?)?(?:;base64)?,([a-z0-9!$&',()*+;=\\-._~:@/?%\\s]*?)\\s*$";

        final Pattern pattern = PatternPool.get(reg, Pattern.DOTALL);
        final Pattern srcPattern = PatternPool.get(srcReg, Pattern.DOTALL);
        final Pattern base64Pattern = PatternPool.get(base64Reg, Pattern.DOTALL);


        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String group = matcher.group();
            System.out.println(group);

            Matcher srcMatcher = srcPattern.matcher(group);
            if(srcMatcher.find()){
                String src = srcMatcher.group(1);
                System.out.println(src);
                if(ReUtil.isMatch(PatternPool.URL_HTTP, src)){
                    System.out.println("isUrl");
                }else if(ReUtil.isMatch(base64Pattern, src)){
                    System.out.println("isBase64");
                }
            }
        }
    }

}
